﻿namespace TestTool.GUI.Controllers
{
    using System;
    using System.Collections.Generic;
    using TestTool.GUI.Data;
    using TestTool.GUI.Enums;
    using TestTool.GUI.Views;

    internal class DeviceController : Controller<IDeviceView>
    {
        private List<IController> _controllers;

        public DeviceController(IDeviceView view) : base(view)
        {
            this._controllers = new List<IController>();
        }

        private void _childController_OperationCompleted()
        {
            base.ReportOperationCompleted();
        }

        private void _childController_OperationStarted()
        {
            base.ReportOperationStarted();
        }

        private void DoWithAll(Action<IController> action)
        {
            foreach (IController controller in this._controllers)
            {
                action(controller);
            }
        }

        public override void Initialize()
        {
            foreach (IController controller in this._controllers)
            {
                controller.Initialize();
            }
        }

        public bool InternalRequestPending()
        {
            foreach (IController controller in this._controllers)
            {
                if (controller.RequestPending)
                {
                    return true;
                }
            }
            return false;
        }

        public override void LoadSavedContext(SavedContext context)
        {
            base.LoadSavedContext(context);
            if (context.DebugInfo != null)
            {
                DebugInfo debugInfo = ContextController.GetDebugInfo();
                debugInfo.CapabilitiesExchange = context.DebugInfo.CapabilitiesExchange;
                debugInfo.Security = context.DebugInfo.Security;
            }
            foreach (IController controller in this._controllers)
            {
                controller.LoadSavedContext(context);
            }
        }

        public void SetChildControllers(DeviceManagementController deviceManagementController, MediaController mediaController, PtzController ptzController, RequestsController requestsController)
        {
            this._controllers.AddRange(new IController[] { deviceManagementController, mediaController, ptzController, requestsController });
            foreach (IController controller in this._controllers)
            {
                controller.OperationStarted += new Action(this._childController_OperationStarted);
                controller.OperationCompleted += new Action(this._childController_OperationCompleted);
            }
        }

        public override void Stop()
        {
            this.DoWithAll(delegate (IController controller) {
                if (controller.RequestPending)
                {
                    controller.Stop();
                }
            });
        }

        public override void SwitchToState(TestTool.GUI.Enums.ApplicationState state)
        {
            this.DoWithAll(controller => controller.SwitchToState(state));
        }

        public override void UpdateContext()
        {
            this.DoWithAll(controller => controller.UpdateContext());
            DebugInfo debugInfo = ContextController.GetDebugInfo();
            debugInfo.CapabilitiesExchange = base.View.CapabilitiesExchange;
            debugInfo.Security = base.View.Security;
        }

        public override void UpdateView()
        {
            this.DoWithAll(controller => controller.UpdateView());
            DebugInfo debugInfo = ContextController.GetDebugInfo();
            base.View.CapabilitiesExchange = debugInfo.CapabilitiesExchange;
            base.View.Security = debugInfo.Security;
        }

        public override void UpdateViewFunctions()
        {
            this.DoWithAll(controller => controller.UpdateViewFunctions());
        }

        public override bool RequestPending
        {
            get
            {
                return this.InternalRequestPending();
            }
        }
    }
}

